package com.ideal.blockchain.controller.deploy;

import com.ideal.blockchain.deploy.Channel;
import com.ideal.blockchain.deploy.Channel1;
import com.ideal.blockchain.dto.response.ResultInfo;
import com.ideal.blockchain.enums.ResponseCodeEnum;
import com.ideal.blockchain.service.deploy.AddChannelServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@RestController
@RequestMapping("/deploy")
public class ChannelDeploy {
    @Autowired
    private AddChannelServiceImpl addChannelService;

    List<Channel1> existChannel=new ArrayList<>();

    @RequestMapping(value = "/addChannel",method = RequestMethod.POST)
    public ResultInfo addChannel(@RequestBody Channel channel){
        System.out.println("addChannel"+channel.toString());
        if (channel.getOrg().length==0){
            return new ResultInfo(ResponseCodeEnum.FAILURE,"请添加组织后再添加通道");
        }

        for (int i = 0; i < existChannel.size(); i++) {
            if(channel.getName().equals(existChannel.get(i).getName())){
                return new ResultInfo(ResponseCodeEnum.FAILURE,"通道名称不能相同");
            }
        }

        Channel1 tmpChannel =new Channel1();
        tmpChannel.setName(channel.getName());
        tmpChannel.setOrgs(StringUtils.join(channel.getOrg(),","));
        try {
            addChannelService.addChannel(tmpChannel);
        }catch (Exception e){
            return new ResultInfo(ResponseCodeEnum.FAILURE, e.getMessage());
        }
        return new ResultInfo(ResponseCodeEnum.SUCCESS, "添加通道成功");
    }

    @RequestMapping("/channelList")
    @ResponseBody
    public List<Channel> channelList(){
        System.out.println("channelList="+addChannelService.queryAllChannel().toString());
        List<Channel> tmpChannel=new ArrayList<>();
        for(int i=0;i<addChannelService.queryAllChannel().size();i++){
            existChannel.add(addChannelService.queryAllChannel().get(i));
            Channel test=new Channel();
            test.setName(addChannelService.queryAllChannel().get(i).getName());
            test.setOrg(addChannelService.queryAllChannel().get(i).getOrgs().split(","));
            tmpChannel.add(test);
        }
        HashSet tmp=new HashSet(existChannel);
        existChannel.clear();
        existChannel.addAll(tmp);
        System.out.println("existChannel"+existChannel);
        return  tmpChannel;
    }

    @RequestMapping(value = "/delChannel",method = RequestMethod.POST)
    public ResultInfo delChannel(@RequestBody Channel1 channel){
        System.out.println("delChannel="+channel.getName());
        try{
            addChannelService.delChannel(channel.getName());
        }catch (Exception e){
            return new ResultInfo(ResponseCodeEnum.FAILURE, e.getMessage());
        }
        return new ResultInfo(ResponseCodeEnum.SUCCESS, "删除通道成功");
    }

}
